package com.example.demo.config;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by void on 2020/4/12.
 * 使用@WebFilter可以将filter加入web请求过滤链,不过需要增加@ServletComponentScan在启动类上
 * 多个filter如果没有指定order,按照字典序
 * urlPatterns可以限制filter生效的路由  (urlPatterns = {"/ip"})
 */
@Slf4j
@WebFilter(filterName = "ipFilter")
public class IpFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;

//        log.info("=========================内部ipFilter========================");
//        HttpUtil.printAllHeader(request);

        log.info("IpFilter.doFilter ip:{}", request.getRemoteAddr());
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
